package com.tian.service.impl;

import com.tian.entity.ChargeInviteRecord;
import com.tian.entity.ChargeUserIncome;
import com.tian.mapper.ChargeInviteRecordMapper;
import com.tian.mapper.ChargeUserIncomeMapper;
import com.tian.message.InvitedRegistryMessage;
import com.tian.service.UserInvitedRegistryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;

/**
 * {@code @description:} TODO
 *
 * @author tianwc 公众号：Java后端技术全栈
 * 在线刷题 1200+java面试题和1000+篇技术文章：<a href="https://woaijava.cc/">博客地址</a>
 * {@code @date:} 2024-03-30 16:30
 * {@code @version:} 1.0
 */
@Slf4j
@Service
public class UserInvitedRegistryServiceImpl implements UserInvitedRegistryService {

    @Resource
    private ChargeUserIncomeMapper chargeUserIncomeMapper;
    @Resource
    private ChargeInviteRecordMapper chargeInviteRecordMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void invitedRegistry(InvitedRegistryMessage invitedRegistryMessage) {
        //防止重复消费
        int count = chargeInviteRecordMapper.countByReqId(invitedRegistryMessage.getReqId());
        if (count > 0) {
            log.info("【重复消费】，请求参数={}", invitedRegistryMessage);
            return;
        }
        //邀请记录
        ChargeInviteRecord chargeInviteRecord = new ChargeInviteRecord();
        Date date = new Date();
        chargeInviteRecord.setInvitedTime(date);
        chargeInviteRecord.setInvitedUserId(invitedRegistryMessage.getNewUserId());
        chargeInviteRecord.setInviteUserId(invitedRegistryMessage.getUserId());
        chargeInviteRecordMapper.insert(chargeInviteRecord);
        if (invitedRegistryMessage.getAmount().compareTo(BigDecimal.ZERO) <= 0) {
            return;
        }
        //邀请收益记录
        ChargeUserIncome chargeUserIncome = new ChargeUserIncome();
        chargeUserIncome.setIncome(invitedRegistryMessage.getAmount());
        chargeUserIncome.setUserId(invitedRegistryMessage.getUserId());
        chargeUserIncome.setCreateTime(date);
        //可以优化：0表示在途收益 1表示已获取收益（被邀请用户存在交易后）
        chargeUserIncome.setType(0);
        chargeUserIncomeMapper.insert(chargeUserIncome);
    }
}
